package com.zuihai.app.mvp.ui.activity.base;

import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.mvp.IPresenter;
import com.zuihai.app.R;
import com.zuihai.app.mvp.ui.widget.ToolbarWrapper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class ToolBarBaseEditActicalFragment<P extends IPresenter> extends BaseFragment<P> {

    @BindView(R.id.toolbar)
    ToolbarWrapper toolbar;

    @BindView(R.id.fl_title)
    FrameLayout frameLayout;

    @BindView(R.id.tv_tobar_title_text)
    TextView tvTitle;

    @BindView(R.id.back_layout)
    FrameLayout backLayout;

    @BindView(R.id.iv_zhanwei)
    ImageView logoView;

    @BindView(R.id.btn_action_one)
    ImageView actionOne;

    @BindView(R.id.btn_action_two)
    ImageView actionTwo;

    @BindView(R.id.btn_action_three)
    ImageView actionThree;
    @BindView(R.id.iv_hint_draft)
    ImageView ivHintDraft;
    @BindView(R.id.btn_action_add_one)
    ImageView btnActionAddOne;
    Unbinder unbinder;

    @Override
    protected final View initView(LayoutInflater inflater, ViewGroup container) {
        //获取ToolbarLayout
        ViewGroup toolbarLayout = (ViewGroup) LayoutInflater.from(getContext())
                .inflate(R.layout.fragment_top_bar_edit_acticalbase, null, false);
        //找到内容容器
        ViewGroup contentContainer = (ViewGroup) toolbarLayout.findViewById(R.id.fl_title);
        //将内容放入到容器中
        LayoutInflater.from(getContext()).inflate(initContentView(), contentContainer, true);
        return toolbarLayout;
    }

    @Override
    protected void initData() {
        initToolbar();
    }

    /**
     * 初始化Toolbar布局
     */
    private void initToolbar() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        backLayout.setVisibility(hasBack() ? View.VISIBLE : View.GONE);
        backLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
    }

//    /**
//     * 返回键监听
//     * @param view
//     */
//    @OnClick(R.id.back_layout)
//    public void onBack(View view){
//    }

    /**
     * 是否有返回
     *
     * @return
     */
    public boolean hasBack() {
        return true;
    }

    protected void setTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        }
    }

    public FrameLayout getBackLayout(){
        return backLayout;
    }

    protected void setTitle(@StringRes int title) {
        tvTitle.setText(title);
        tvTitle.setVisibility(View.VISIBLE);
    }

    public void setLogo(@DrawableRes int drawable) {
        logoView.setImageResource(drawable);
        logoView.setVisibility(View.VISIBLE);
    }


    public ToolbarWrapper getToolbar() {
        return toolbar;
    }

    public void setupActionThree(@DrawableRes int drawable, View.OnClickListener listener) {
        actionThree.setVisibility(View.VISIBLE);
        actionThree.setImageResource(drawable);
        actionThree.setOnClickListener(listener);
    }

    public void setupActionTwo(@DrawableRes int drawable, View.OnClickListener listener) {
        actionTwo.setVisibility(View.VISIBLE);
        actionTwo.setImageResource(drawable);
        actionTwo.setOnClickListener(listener);
    }

    public void setupActionOne(@DrawableRes int drawable, View.OnClickListener listener) {
        actionOne.setVisibility(View.VISIBLE);
        actionOne.setBackgroundResource(drawable);
        actionOne.setOnClickListener(listener);
    }

    public ImageView getImageViewHint() {
        return ivHintDraft;
    }

    public ImageView getActionThree() {
        return actionThree;
    }
    public ImageView getActionTwo() {
        return actionTwo;
    }

    protected abstract int initContentView();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
